package com.jkkc.lib.base.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;

public final class ScreenUtils {

    // 取屏幕密度
    public static float getScreenDensity(Activity context) {

        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.density;
    }

    // 取屏幕高度
    public static int getScreenHeight(Activity context) {

        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    @SuppressWarnings("deprecation")
    public static int getDisplayHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getHeight();
    }

    // 取屏幕宽度
    public static int getScreenWidth(Activity context) {

        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @SuppressWarnings("deprecation")
    public static int getDisplayWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    // 隱藏title
    public static void hideTitle(Activity context) {
        context.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    // 是否全屏
    public static void makeFullScreen(Activity context, Boolean isFullScreen) {
        if (isFullScreen) {
            context.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            context.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        }
    }

    // 状态栏的高度
    public static int getStatusBarHeight(Activity context) {

        Rect mainFrame = new Rect();
        context.getWindow().getDecorView().getWindowVisibleDisplayFrame(mainFrame);
        return mainFrame.top;
    }

    /**
     * 4.0以后版本 获取状态栏高度
     *
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        int statusBarHeight = 38;// 默认为38，貌似大部分是这样的
        try {
            Class<?> cls = Class.forName("com.android.internal.R$dimen");
            Object obj = cls.newInstance();
            Field field = cls.getField("status_bar_height");
            int value = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(value);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    public static float dp2px(Context context, float dp) {
        if (context == null) {
            return -1;
        }
        return dp * context.getResources().getDisplayMetrics().density;
    }

    public static float px2dp(Context context, float px) {
        if (context == null) {
            return -1;
        }
        return px / context.getResources().getDisplayMetrics().density;
    }

    public static int dp2pxInt(Context context, float dp) {
        return (int) (dp2px(context, dp) + 0.5f);
    }

    public static int px2dpInt(Context context, float px) {
        return (int) (px2dp(context, px) + 0.5f);
    }

    /**
     * 隐藏底部home键(第二次出来后不能自动隐藏)
     *
     * @param ac
     * @param isHide
     */
    public static void hideNavigationBar(Activity ac, boolean isHide) {
        if (null == ac) {
            return;
        }
        Window window = ac.getWindow();
        if (null == window) {
            return;
        }
        View decorView = window.getDecorView();
        if (null == decorView) {
            return;
        }
        int flags;
        int curApiVersion = Build.VERSION.SDK_INT;
        // This work only for android 4.4+
        if (curApiVersion >= Build.VERSION_CODES.KITKAT) {
            // This work only for android 4.4+
            // hide navigation bar permanently in android activity
            // touch the screen, the navigation bar will not show
            if (isHide) {
                flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;
            } else {
                flags = View.SYSTEM_UI_FLAG_FULLSCREEN;
            }

        } else {
            // touch the screen, the navigation bar will show
            flags = View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }

        decorView.setSystemUiVisibility(flags);
        //decorView.setSystemUiVisibility(4108);
    }

    /**
     * (第二次出来后可以自动隐藏)
     *
     * @param ac
     */
    public static void hideNavigationBar(Activity ac) {
        if (null == ac) {
            return;
        }
        Window window = ac.getWindow();
        if (null == window) {
            return;
        }
        WindowManager.LayoutParams params = window.getAttributes();
        if (null == params) {
            return;
        }
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        window.setAttributes(params);
    }


}
